<?php
//error_reporting("E_ALL");ini_set("display_errors", 1);
header("content-Type: application/json; charset=utf-8");
include("include/function.php");
include("include/OllamaClient.php");
$config = include("config.php");
$post = !empty($_POST)?$_POST:$_GET;
if(empty($post)){
    exit('?country=美国|中国&model=deepseek&page=1&apikey=xxx');
}
if(empty($post['apikey']) || !in_array($post['apikey'],$config['API_KEYS'])){
    $return = array('status'=>0,'msg'=>'apikey错误');ajaxReturn($return);
}
include("include/".$config['DB_TYPE'].".class.php");
$db = new db;
$db->connect($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'], $config['DB_PCONNECT']);

$page = !empty($post['page'])?intval($post['page']):1;
$pagesize = 50;
$start = ($page-1)*$pagesize;
$where = 'status=1 and is_delete=0';
if(!empty($post['country'])){
    $where.=" and country_name like '%".htmlspecialchars($post['country'])."%'";
}
if(!empty($post['model'])){
    $where.=" and model like '%".htmlspecialchars($post['model'])."%'";
}
$count = $db->get_one("select count(id) as total from ollama_list where ".$where);
if(!empty($count) && $count['total']>0){
    $field = 'host,name,model,country_name as country,haoshi as time,addtime,FROM_UNIXTIME(`updatetime`) as updatetime';
    if(!empty($post['all'])){
        $field='host,name,model,country_name as country,error as content,modified_at,size,details,haoshi as time,addtime,FROM_UNIXTIME(`updatetime`) as updatetime';
    }
    $sql="select ".$field." from ollama_list where ".$where." order by haoshi limit ".$start.','.$pagesize;
    $list = $db->get_all($sql);$total=$count['total'];
}else{
    $list='';
}
if(!empty($list)){
    $return = array('status'=>1,'data'=>$list,'total'=>$total);
}else{
    $return = array('status'=>0,'msg'=>'没有获取数据');
}
ajaxReturn($return);
